package exam;

import exam.TreeNode;
import java.util.ArrayList;
import java.util.List;

/**
 * Tests the answer to the third tree question.
 * The text of the question is as follows:
 * "Write a toString method for a binary search tree using in-order traversal 
 * given the root of the tree.  This should include the data in each node of the tree."
 * 
 * @author Branden Ogata
 *
 */

public class TestTreeA02 extends Tester
{
  public static void main(String[] args)
  {
    final double POINTS = getPoints(args);
    
    int[] values = new int[] {4, 2, 6, 1, 3, 5, 7};
    List<TreeNode<Integer>> nodes = new ArrayList<TreeNode<Integer>>();

    for (int i = 0; i < values.length; i++)
    {
      nodes.add(new TreeNode<Integer>(values[i]));
    }

    // Add in the links between nodes
    for (int i = 0; i < values.length; i++)
    {
      // If a left child exists
      if (((2 * i) + 1) < values.length)
      {
        nodes.get(i).setLeft(nodes.get((2 * i) + 1));
      }
      
      // If a right child exists
      if (((2 * i) + 2) < values.length)
      {
        nodes.get(i).setRight(nodes.get((2 * i) + 2));        
      }
    }
    
    TreeNode<Integer> root = nodes.get(0);
    
    // Create the regex for the toString result
    StringBuilder builder = new StringBuilder();
    builder.append(".*");
    
    for (int j = 0; j < nodes.size(); j++)
    {
      builder.append(j + 1 + ".*");
    }
    
    String regex = builder.toString();
    
    System.out.println(test(Category.EQUALS_REGEX, "toString(root)", 
                            regex, TreeA02.toString(root), 
                            percentageToPoints(1, POINTS)));    
    
    System.out.format("[%.2f/%.2f]%n", getTotalPoints(), POINTS);
  }
}
